import {View, Image, Platform,DeviceEventEmitter} from 'react-native';
import mqtt from '../common/mqtt'
const mqttChat=(id,isDelete)=>{
    if(isDelete){
        this.client.end();
    }else {
        const group = id;
        const params = {
            group,
            clean: true,
            username: 'mosquitto',
            password: '123',
            reconnectPeriod: 3000, // 重连
        };
        this.client = mqtt.connect('ws://106.14.209.177:9000', params);
        //链接系统
        this.client.on('connect', (res) => {
            const topic = {};
            topic[`layout/customer${id}/#`] = 1;
            this.client.subscribe(topic);
        });
        this.client.on('reconnect', (res) => {
            const topic = {};
            topic[`layout/customer${id}/#`] = 1;
            this.client.subscribe(topic);
        });
        // 接收消息
        this.client.on('message', (topic, message) => {
            const msgStr = message.toString();
            const msg = JSON.parse(msgStr);
            DeviceEventEmitter.emit('IM',msg);
        });
        // 连接失败
        this.client.on('error', (error) => {
            console.log(error)
        });
    }
};
export default mqttChat